Skip to content

fix: guard JDTUtils.searchDecompiledSources NPE via OSGi WeavingHook#22

Open
runchen0919 wants to merge 1 commit into
monkey666-cr:mainfrom
runchen0919:fix/weaving-hook-npe-guard
Open

fix: guard JDTUtils.searchDecompiledSources NPE via OSGi WeavingHook#22
runchen0919 wants to merge 1 commit into
monkey666-cr:mainfrom
runchen0919:fix/weaving-hook-npe-guard

Conversation

@runchen0919
Copy link
Copy Markdown
Contributor

Summary

  • Registers an OSGi WeavingHook to patch JDT.LS handler classes at load time, wrapping searchDecompiledSources call sites in try-catch(NPE) to return Collections.emptyList() instead of crashing
  • Fixes "Find References" and "Go to Implementation" failures when classpath JARs lack source attachments (upstream bug: eclipse-jdtls#3083)
  • Patches callers (not JDTUtils itself) because JDTUtils is loaded before our bundle starts, while handler classes are lazily loaded on first LSP request

Changes

File Change
java-bridge/pom.xml Added org.ow2.asm:asm:9.7 (compile) and org.osgi:osgi.core:8.0.0 (provided)
java-bridge/bnd.bnd Added org.osgi.framework.hooks.weaving import, Private-Package for ASM embedding
JDTUtilsPatcher.java New — WeavingHook with pre-scan filter + SafeClassWriter for OSGi compatibility
BazelActivator.java Register/unregister WeavingHook in start()/stop()
JDTUtilsPatcherTest.java New — 4 unit tests including end-to-end NPE→emptyList verification
docs/weaving-hook-npe-fix.md New — Detailed design documentation

Key Design Choices

  • Pre-scan optimization: Read-only ASM scan with SKIP_DEBUG | SKIP_FRAMES to skip classes without the target call site, avoiding ClassNotFoundException from COMPUTE_FRAMES in OSGi
  • SafeClassWriter: Falls back to java/lang/Object when cross-bundle type resolution fails
  • Version resilient: Matches by owner+method name only (no descriptor), works across JDT.LS versions
  • Graceful degradation: If patch fails or upstream fixes the bug, no regression — just a no-op

Test plan

  • 4 unit tests pass (mvn test -Dtest=JDTUtilsPatcherTest)
  • Existing tests unaffected
  • mvn compile succeeds, ASM classes embedded in bundle
  • MANIFEST.MF contains org.osgi.framework.hooks.weaving import
  • Manual E2E: "Find References" no longer crashes on JARs without source attachment
  • No Failed to patch warnings in JDT.LS logs

🤖 Generated with Claude Code

JDT.LS has an unfixed bug (eclipse-jdtls#3083) where
JDTUtils.searchDecompiledSources() throws NPE in the MethodInvocation
branch due to a missing finder.initialize() call. This crashes "Find
References" and "Go to Implementation" when classpath JARs lack source
attachments.

This fix registers an OSGi WeavingHook that intercepts class loading of
JDT.LS handler classes and wraps call sites to searchDecompiledSources
in try-catch(NPE), returning Collections.emptyList() on catch. The
approach patches callers (not JDTUtils itself) because JDTUtils is
loaded during JDT.LS initialization before our bundle starts, while
handler classes are lazily loaded on first LSP request.

Key design choices:
- Pre-scan classes with bare ClassVisitor before COMPUTE_FRAMES to
  avoid ClassNotFoundException in OSGi for unrelated classes
- SafeClassWriter falls back to java/lang/Object when type resolution
  fails across bundle boundaries
- Match by owner+method name only (no descriptor) for version resilience
- ASM 9.7 embedded as Private-Package via bnd

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant